<?php
class Mvehicle extends CI_Model {

    private $_table = 'vehicle';
    public function __construct() {
        parent::__construct();
    }
    
    /**
     * Get the list of vehicles
     */
    public function listAllVehicles($record=null, $start=null, $order=null) {
        if ($record != null && $start != null) {
            $this->db->limit($record, $start);
        }
        if ($order != null) {
            $this->db->order_by($order);
        }
        $query = $this->db->get($this->_table);        
        return $query->result_array();
    }
    
    public function insertVehicle($data) {
        $this->db->insert($this->_table, $data);
        return $this;
    }
    
    public function updateVehicle($data_update, $id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, $data_update);
        return $this;
    }
    
    public function deleteVehicle($id) {
        $this->db->where('id', $id)->delete($this->_table);
        return $this;
    }
    
    public function getVehicleById($id) {
        $this->db->where('id', $id);
        return $this->db->get($this->_table)->row_array();
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function listVehicleByLineId($id) {
        $this->db->where('line_id', $id);
        return $this->db->get($this->_table)->result_array();
    }
    
    public function deleteSetVehicle($id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, array('line_id' => 0));
    }
    
    public function deletePositionByLineId($line_id) {
        $this->db->where('line_id', $line_id);
        $this->db->update($this->_table, array('position_station' => 'INTER'));
        return $this;
    }
    
    public function setVehicle($line_id, $id) {
        $this->db->where('id', $id);
        $this->db->update($this->_table, array('line_id' => $line_id));
        return $this;
    }

}